<?php

/**
 * 汽车加油
 * 作者：*****
 */


namespace App\model;

use Illuminate\Database\Eloquent\Model;

use DB;
class Oil extends Model
{
    protected $table='oil';

    /**
     * 汽油柱状图
     *
     */

    public function oil($date)
    {
    	  $arr=$this->where('oil_year',$date)
                  ->orderBy('oil.oil_month','asc')
                  ->get()
                  ->toArray();
        
        $info=array();

        foreach ($arr as $k => $v) 
        {

          $str=$this->join('oil_info','oil_info.oil_id', '=', 'oil.oil_id')
                    ->join('car_adm', 'car_adm.c_id', '=', 'oil_info.c_id')
                    ->where('oil_info.oil_id',$v['oil_id'])
                    ->select('o_num')
                    ->get()
                    ->toArray();

            $count=array();

           foreach ($str as $key => $val) 
           {
               $count[]=$val['o_num'];
           }

           $oil['mouth']=$arr[$k];
           $oil['mouth']['info']=array_sum($count);
           $info[]=$oil;

        }

        $oil=DB::table('oil_type')->get();

        $oilInfo=array();

        foreach ($oil as $kk=> $vv) 
        {
           $oil_num=$vv->oil_num;
           foreach ($info as $key => $vvv) {

            if($oil_num==$vvv['mouth']['oil_pid'])
            {
               
               $mouth[]=$info[$key]['mouth'];
              $oilInfo[$vv->oil_num]=$mouth;
            }
           
          }


        }


        foreach ($oilInfo as $key => $value) 
        {
            

            foreach ($value as $kk => $vv) 
            {
               
                if ($vv['oil_pid']!=$key) 
                {
                   
                    unset($oilInfo[$key][$kk]);
                   
                }
            }

            $oilInfo[$key]=array_merge($oilInfo[$key]); 
        }
                       
            return $oilInfo;     
    }

    /**
     * 汽油详情查询记录
     *
     */

    public function oillist()
    {
        return $this
                    ->join('oil_info','oil_info.oil_id', '=', 'oil.oil_id')
                    ->join('car_adm', 'car_adm.c_id', '=', 'oil_info.c_id')
                    ->paginate(5);                  
                    
    }

}
